package org.beanutopia.htmap;

import org.beanutopia.Property;
import org.beanutopia.UpdatableProperty;

import java.io.Serializable;
import java.util.IdentityHashMap;

/**
 * @author ymeymann
 * @since Apr 30, 2007 2:54:28 PM
 */
public class HTMap implements Cloneable, Serializable, HasProperties {
  private static final long serialVersionUID = -3310029320719869501L;

  private IdentityHashMap<Property<?,?>, Object> storage =
      new IdentityHashMap<Property<?,?>, Object>();

  <V> V getProperty(Property<?,V> property) {
    return property.valueType().cast(storage.get(property));
  }

  @SuppressWarnings({"unchecked"})
  <V> void setProperty(UpdatableProperty<?,V> property, V value) {
    if (value == null)
      storage.remove(property);
    else
      storage.put(property, value);
//    setPropertyValue(property.value(value));
  }

//  void setPropertyValue(UpdatablePropertyValue propertyValue) {
//    put(propertyValue.getProperty(), propertyValue.getValue());
//  }

  public final HTMap bean() {
    return this;
  }

  @SuppressWarnings({"CloneDoesntDeclareCloneNotSupportedException"})
  public HTMap clone() {
    HTMap clone = null;
    try {
      clone = (HTMap) super.clone();
    } catch (CloneNotSupportedException e) {
      clone = new HTMap();
    }
    clone.storage = (IdentityHashMap) this.storage.clone();
    return clone;
  }

}
